  var socket;
  if (!window.WebSocket) {
    window.WebSocket = window.MozWebSocket;
  }
  if (window.WebSocket) {
    socket = new WebSocket("ws://localhost:2048/ws");
    socket.onmessage = function(event) {
      var ta = document.getElementById('responseText');
      ta.value = ta.value + '\n' + event.data
    };
    socket.onopen = function(event) {
      var ta = document.getElementById('responseText');
      ta.value = "连接开启!";
    };
    socket.onclose = function(event) {
      var ta = document.getElementById('responseText');
      ta.value = ta.value + "连接被关闭";
    };
  } else {
    alert("你的浏览器不支持！");
  }

  function send_bak(message) {
    if (!window.WebSocket) {
       return;
    }
    if (socket.readyState == WebSocket.OPEN) {
      socket.send(message);
    } else {
      alert("连接没有开启.");
    }
  }

  function isCheck() {
    var check = $("#lightSwitch")
    if(check.attr("checked")){
      send("open")
    }else {
      send("close")
    }
  }

  function send(message) {
    if (!window.WebSocket) {
       return;
    }
    if (socket.readyState == WebSocket.OPEN) {
      if(message === "light"){
        protobuf.load("HardWare.proto",function (err,root) {
            if(err) throw err;
            var lightRequest = root.lookup("CommandEntity.LightRequest");
            var version = lightRequest.create({version:"10001",cid:"Do992992"});
            var versionBuffer = lightRequest.encode(version).finish();
            var decode =  lightRequest.decode(versionBuffer);
            console.log("version========"+version);
            console.log("versionBuffer========"+versionBuffer);
            console.log("lightRequest========"+lightRequest);
            console.log("decode========"+decode);
            socket.send(versionBuffer)
        });
      }else {
        socket.send(message);
      }
    } else {
      alert("连接没有开启.");
    }
  }
